/**
 * Created by MadFrog on 2017/7/5.
 */

Ext.namespace("admin.event");

admin.event.inspect = function(panelId, title, iconClz){
    var eventStore = new Ext.data.JsonStore({
        url : "admin/event/findByCon",
        fields : ["id", "name", "description", "eventDate", "startTime",
            "endTime", "location", "maxSeat", "status", "createDate", "updateDate"],
        totalProperty : "total",
        root : "data",
        autoLoad : true,
        listeners : {
            beforeload : function(s){
                s.removeAll();
                Ext.apply(s.baseParams, {
                    keyword : queryField.getValue(),
                    status : statusCombo.getValue()
                });
            }
        }
    });
    var eventSm = new Ext.grid.RowSelectionModel({
        singleSelect : true
    });
    var eventCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header : "活动名称",
        dataIndex : "name",
        width : 200
    }, {
        header : "地点",
        dataIndex : "location",
        width : 150
    }, {
        header : "活动时间",
        dataIndex : "eventDate",
        renderer : renderTime2Minutes,
        width : 110
    }]);
    var statusCombo = genStatusCombo();
    var queryField = genQueryField();
    var queryBar = ["->", "状态：", statusCombo, "关键词：",  queryField, {
        text : "查询",
        iconCls : "query",
        handler : function(){
            eventStore.load();
        }
    }];
    var eventGrid = new Ext.grid.GridPanel({
        title : "活动列表",
        store : eventStore,
        cm : eventCm,
        sm : eventSm,
        frame : true,
        flex : 1,
        tbar : ["->", queryBar],
        bbar : genPageBar(eventStore)
    });
    var studentStore = new Ext.data.JsonStore({
        url : "admin/studentEvent/listStudentByCon",
        fields : ["id", "name", "xb", "xh"],
        // totalProperty : "total",
        root : "data",
        listeners : {
            beforeload : function(s){
                var eventId;
                var stuRec = eventSm.getSelected();
                var clazzId = clazzCombo.getValue();
                var attendStatus = attendStatusCombo.getValue()
                if(stuRec==undefined){
                    showFailMsg("请选择活动");
                    return false;
                }
                if(clazzId===""){
                    showFailMsg("请选择班级");
                    return false;
                }
                if(attendStatus===""){
                    showFailMsg("请选择参加状态")
                    return false;
                }
                s.removeAll();
                eventId = stuRec.get("id");
                Ext.apply(s.baseParams, {
                    eventId : eventId,
                    clazzId : clazzId,
                    attendStatus : attendStatus
                });
            }
        }
    });
    var studentSm = new Ext.grid.RowSelectionModel({
        singleSelect : true
    });
    var studentCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header : "姓名",
        dataIndex : "name"
    }, {
        header : "性别",
        dataIndex : "xb",
        width : 50,
        renderer : renderEnumGender
    }, {
        header : "学号",
        dataIndex : "xh"
    }]);
    var clazzCombo = ExtComponentUtil.generateClassComboBox();
    var attendStatusCombo = ExtComponentUtil.generateAttendStatusComboBox();
    var queryField = genQueryField();
    var queryBar = ["->", "班级:",  clazzCombo, "状态：", attendStatusCombo, {
        text : "查询",
        iconCls : "query",
        handler : function(){
            studentStore.load();
        }
    }];
    var studentGrid = new Ext.grid.GridPanel({
        title : "学生列表",
        store : studentStore,
        cm : studentCm,
        sm : studentSm,
        frame : true,
        flex : 1,
        tbar : ["->", queryBar]
    });
    return new Ext.Panel({
        id : panelId,
        title : title,
        iconCls : iconClz,
        closable : true,
        layout : "hbox",
        layoutConfig : {
            align : "stretch"
        },
        items : [eventGrid, studentGrid]
        ,
        tbar : genToolBar([{
            text : "导出",
            iconCls : "export",
            handler : exportOut
        }], [], panelId)
    });
    
    function exportOut() {
        var rec = eventSm.getSelected();
        if(rec===undefined){
            showFailMsg("请选择活动");
            return;
        }
        var eventId = rec.get("id");
        window.open("admin/studentEvent/exportOut/" + eventId);
    }
}

registerModule("admin.event.inspect", admin.event.inspect);
